package main

import (
	"bufio"
	"crypto/md5"
	"flag"
	"fmt"
	"io"
	"os"
	"time"
)

var file_src *string = flag.String("file", "", "The file to hash")

const bufferSize = 65536

// MD5sum returns MD5 checksum of filename
func MD5sum(filename string) (string, error) {
	if info, err := os.Stat(filename); err != nil {
		return "", err
	} else if info.IsDir() {
		return "", nil
	}

	file, err := os.Open(filename)
	if err != nil {
		return "", err
	}
	defer file.Close()

	hash := md5.New()
	for buf, reader := make([]byte, bufferSize), bufio.NewReader(file); ; {
		n, err := reader.Read(buf)
		if err != nil {
			if err == io.EOF {
				break
			}
			return "", err
		}

		hash.Write(buf[:n])
	}

	checksum := fmt.Sprintf("%x", hash.Sum(nil))
	return checksum, nil
}

/*
func main() {

	flag.Parse()
	fmt.Println("Opening file:" + *file_src)

	t1 := time.Now()
	//filepath := "/Users/liguanghui/Documents/IIT/IIT-ECE449.ova"
	filepath := "/Users/liguanghui/Documents/资料/立志励志/admaid_visit_campus_800x650.jpg"

	md5sum, _ := MD5sum(filepath)
	fmt.Println(md5sum)

	t2 := time.Now()
	fmt.Println("消耗时间：", t2.Sub(t1), "秒")

}

*/
